package com.hq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.bean.Mingxi;
import com.hq.bean.Order;
import com.hq.bean.User;
import com.hq.service.OrderService;
import com.hq.util.FileUploadUtil;
import com.hq.util.SnowflakeIdUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {
    private String snowid="";
    @Resource
    private OrderService orderService;
    @RequestMapping("list.do")
    public String list(@RequestParam(value = "cpage",defaultValue = "1")Integer cpage,
                       String mmid,
                       String kuqu,
                       String wuliaobianhao,
                       Integer priceX,
                       Integer priceD,
                       Integer numX,
                       Integer numD,
                       Integer zongjineX,
                       Integer zongjineD,
                       Integer id,
                       Model model,
                       HttpSession session){
        session.setAttribute("id",id);
        HashMap map = new HashMap();
        map.put("mmid",mmid);
        map.put("kuqu",kuqu);
        map.put("wuliaobianhao",wuliaobianhao);
        map.put("priceX",priceX);
        map.put("priceD",priceD);
        map.put("numX",numX);
        map.put("numD",numD);
        map.put("zongjineX",zongjineX);
        map.put("zongjineD",zongjineD);
        map.put("id",id);
        List list = orderService.list(map);
//        PageHelper.startPage(cpage,3);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }
    @RequestMapping("toupd.do")
    public String toupd(HttpServletRequest request,Model model){
        Object id = request.getSession().getAttribute("id");
        model.addAttribute("id",id);
        return "add";
    }
    @RequestMapping("getsnowid.do")
    @ResponseBody
    public Object getsnowid(){
        orderService.getsnowid(snowid);
        long l = new SnowflakeIdUtils(1, 1).nextId();
        snowid=l+"";
        return snowid;
    }
    @RequestMapping("addmi.do")
    public Object addmi(Mingxi mingxi,Model model){
        int getsnowid = orderService.getsnowid(mingxi.getMmid());
        if (getsnowid==0){
            orderService.add(mingxi.getMmid());
        }
        orderService.addmi(mingxi);
        model.addAttribute("snowid",mingxi.getMmid());
        return "add";
    }
    @RequestMapping("apendList.do")
    @ResponseBody
    public Object apendList(String snowid){
        List list = orderService.apendList(snowid);
        return list;
    }
    @RequestMapping("updadd.do")
    @ResponseBody
    public Object updadd(Order order, HttpSession session, MultipartFile file){
        if (file.getSize()>0){
            String upload = FileUploadUtil.load(session, file, "upload");
            order.setPic(upload);
        }
        int updadd = orderService.updadd(order);
        return updadd;
    }
    @RequestMapping("login.do")
    public String login(User user,HttpSession session){
        User login = orderService.login(user);
        if (login!=null){
            session.setAttribute("user",user);
            return "redirect:list.do";
        }else {
            return "redirect:index.jsp";
        }
    }
    @RequestMapping("xiut.do")
    public Object xiut(){
        return "upd";
    }


    @RequestMapping("hui.do")
    @ResponseBody
    public Object hui(String oid){
        Map hui = orderService.hui(oid);
        return hui;
    }
    @RequestMapping("huimingxi.do")
    @ResponseBody
    public Object huimingxi(String mmid){
        Map huimingxi = orderService.huimingxi(mmid);
        return huimingxi;
    }
    @RequestMapping("updorder.do")
    @ResponseBody
    public Object updorder(Order order, HttpSession session, MultipartFile file){
        if (file.getSize()>0){
            String upload = FileUploadUtil.load(session, file, "upload");
            order.setPic(upload);
        }
        int updorder = orderService.updorder(order);
        return updorder;
    }
    @RequestMapping("del.do")
    @ResponseBody
    public Object del(String snowid){
        int del = orderService.del(snowid);
        return del;
    }
    @RequestMapping("list1.do")
    public String list1(@RequestParam(value = "cpage",defaultValue = "1")Integer cpage,
                       String mmid,
                       String kuqu,
                       String wuliaobianhao,
                       Integer priceX,
                       Integer priceD,
                       Integer numX,
                       Integer numD,
                       Integer zongjineX,
                       Integer zongjineD,
                       Integer id,
                       Model model,
                       HttpSession session){
        session.setAttribute("id",id);
        HashMap map = new HashMap();
        map.put("mmid",mmid);
        map.put("kuqu",kuqu);
        map.put("wuliaobianhao",wuliaobianhao);
        map.put("priceX",priceX);
        map.put("priceD",priceD);
        map.put("numX",numX);
        map.put("numD",numD);
        map.put("zongjineX",zongjineX);
        map.put("zongjineD",zongjineD);
        map.put("id",id);
        List list = orderService.list1(map);
//        PageHelper.startPage(cpage,3);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }
    @RequestMapping("list2.do")
    public String list2(@RequestParam(value = "cpage",defaultValue = "1")Integer cpage,
                        String mmid,
                        String kuqu,
                        String wuliaobianhao,
                        Integer priceX,
                        Integer priceD,
                        Integer numX,
                        Integer numD,
                        Integer zongjineX,
                        Integer zongjineD,
                        Integer id,
                        Model model,
                        HttpSession session){
        session.setAttribute("id",id);
        HashMap map = new HashMap();
        map.put("mmid",mmid);
        map.put("kuqu",kuqu);
        map.put("wuliaobianhao",wuliaobianhao);
        map.put("priceX",priceX);
        map.put("priceD",priceD);
        map.put("numX",numX);
        map.put("numD",numD);
        map.put("zongjineX",zongjineX);
        map.put("zongjineD",zongjineD);
        map.put("id",id);
        List list = orderService.list2(map);
//        PageHelper.startPage(cpage,3);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }
    @RequestMapping("list3.do")
    public String list3(@RequestParam(value = "cpage",defaultValue = "1")Integer cpage,
                        String mmid,
                        String kuqu,
                        String wuliaobianhao,
                        Integer priceX,
                        Integer priceD,
                        Integer numX,
                        Integer numD,
                        Integer zongjineX,
                        Integer zongjineD,
                        Integer id,
                        Model model,
                        HttpSession session){
        session.setAttribute("id",id);
        HashMap map = new HashMap();
        map.put("mmid",mmid);
        map.put("kuqu",kuqu);
        map.put("wuliaobianhao",wuliaobianhao);
        map.put("priceX",priceX);
        map.put("priceD",priceD);
        map.put("numX",numX);
        map.put("numD",numD);
        map.put("zongjineX",zongjineX);
        map.put("zongjineD",zongjineD);
        map.put("id",id);
        List list = orderService.list3(map);
//        PageHelper.startPage(cpage,3);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }

    @RequestMapping("updwupin.do")
    public Object updwupin(){
        return "listwupin";
    }

}
